package com.example.activelifecycletest.message;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.widget.Button;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import com.example.activelifecycletest.R;

/**
 * 系统通知
 * @author zhangding
 * @date 2022年04月26日16:07:03
 */
public class MessageActivity extends AppCompatActivity {
    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_inform_main);
        Button sendMessage = (Button) findViewById(R.id.send_message);
        sendMessage.setOnClickListener((view) -> {
            switch (view.getId()){
                case R.id.send_message:
                    Notification notification = null;
                    NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
                    // 判断安卓sdk版本
                    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
                        String id = "channelId";
                        String name = "channelName";
                        NotificationChannel channel = new NotificationChannel(id,name,NotificationManager.IMPORTANCE_LOW);
                        manager.createNotificationChannel(channel);
                        notification = new Notification.Builder(this)
                                .setChannelId(id)
                                .setContentTitle("这是标题")
                                .setContentText("这是内容")
                                .setWhen(System.currentTimeMillis())
                                .setSmallIcon(R.mipmap.ic_launcher)
                                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                                .build();
                    }else{
                        notification = new NotificationCompat.Builder(this)
                                .setContentTitle("This is content title")
                                .setContentText("This is content text")
                                .setWhen(System.currentTimeMillis())
                                .setSmallIcon(R.mipmap.ic_launcher)
                                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                                .build();
                    }
                    manager.notify(1,notification);

                    break;
                default:
                    break;

            }
        });
    }
}
